﻿using System;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [CustomEditor(typeof(CameraCurveConfig))]
    public class CameraCurveConfigInspector : Editor
    {
        private SerializedProperty rootRotationProperty;
        private SerializedProperty posProperty;
        private SerializedProperty rotProperty;
        private SerializedProperty targetPosProperty;
        private SerializedProperty targetRotProperty;
        private SerializedProperty fovProperty;
        private SerializedProperty durationProperty;

        private bool[] flod = new bool[64];

        private void OnEnable()
        {
            this.rootRotationProperty = serializedObject.FindProperty("rootRotation");
            this.posProperty = serializedObject.FindProperty("pos");
            this.rotProperty = serializedObject.FindProperty("rot");
            this.targetPosProperty = serializedObject.FindProperty("targetPos");
            this.targetRotProperty = serializedObject.FindProperty("targetRot");
            this.fovProperty = serializedObject.FindProperty("fov");
            this.durationProperty = serializedObject.FindProperty("duration");
        }

        public override void OnInspectorGUI()
        {
            EditorGUILayout.PropertyField(this.durationProperty);
            EditorGUILayout.PropertyField(this.rootRotationProperty);
            EditorGUILayout.PropertyField(this.fovProperty);
            EditorGUILayout.Space(10);
            this.DrawCurveArr(this.posProperty, "Pos");
            this.DrawCurveArr(this.rotProperty, "Rot");
            if (this.targetPosProperty.arraySize > 0)
            {
                EditorGUILayout.Space(10);
                for (int i = 0; i < this.targetPosProperty.arraySize; i++)
                {
                    this.flod[i] = !EditorGUILayout.Foldout(!this.flod[i], new GUIContent($"Target{i + 1}"));
                    if (!this.flod[i])
                    {
                        EditorGUI.indentLevel++;
                        this.DrawCurveArr(this.targetPosProperty.GetArrayElementAtIndex(i), "Pos");
                        this.DrawCurveArr(this.targetRotProperty.GetArrayElementAtIndex(i), "Rot");
                        EditorGUI.indentLevel--;
                    }
                }
            }

            this.serializedObject.ApplyModifiedProperties();
        }

        private void DrawCurveArr(SerializedProperty arrProperty, string label)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(label, GUILayout.MinWidth(30), GUILayout.MaxWidth(50));
            EditorGUILayout.BeginVertical();
            var rArrValue = arrProperty.FindPropertyRelative("value");
            for (int i = 0; i < rArrValue.arraySize; i++)
            {
                var rStr = i switch
                {
                    0 => "x",
                    1 => "y",
                    2 => "z",
                    3 => "w",
                    _ => ""
                };
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(rStr, GUILayout.Width(15));
                var rValue = rArrValue.GetArrayElementAtIndex(i);
                EditorGUILayout.PropertyField(rValue, new GUIContent(""));
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();
        }
    }
}